import { gameConfigHandle } from "core/config/gameConfig"
import gameUtils from "core/utils/gameUtils"
import Random from "core/utils/random"
import utils from "core/utils/utils"
import { MarketItemModel } from "core/model/marketModel"
import { PlayerModel } from "core/model/playerModel"
import skillHandle from "./skillHandle"
import equipmentHandle from "./equipmentHandel"
import petHandle from "./petHandle"
import adventureHandle from "./adventureHandle"

class MarketHandle {
    shopgetItemByLvl(items: any[], lvl) {
        let data = []
        items.forEach(item => {
            // if (lvl >= item.limitLvl) {
                data.push(item)
            // }
        })
        return data
    }
    create(player: PlayerModel, rand: Random) { //黑市
        let marketConfig = gameConfigHandle.gameConfig.market
        let { maxTotal } = adventureHandle.getAdventureMapProgress(player)
        let data: MarketItemModel[] = []
        let gemItems = this.shopgetItemByLvl(marketConfig.gemItems, maxTotal)
        let items = this.shopgetItemByLvl(marketConfig.items, marketConfig.items.length)

        // items = utils.getRandomArrayNoRepeat(items, marketConfig.items.length, rand)
        let itemList = [...items]

        itemList.forEach(item => {
            if (maxTotal >= item.limitLvl) {
                let amount = 1
                let diamond = item.diamond
                let marketItem = new MarketItemModel()
                if (item.amount) {
                    amount = item.amount
                    diamond = item.diamond
                } else if (item.resources) {
                    let lvl = Math.floor(maxTotal / 5)
                    if (lvl > 20) {
                        lvl = 20
                    }
                    amount = gameUtils.getResourceAmount(item.item, maxTotal) * utils.getIncValues(item.resources, lvl)
                    diamond = utils.getIncValues(item.diamond, 1)
                }
                if (item.skillBook) {
                    marketItem.item = skillHandle.createBook(rand, { lucky: player.market.lvl * 5, minQuality: 1 })
                    diamond = utils.getIncValues(item.diamond, marketItem.item.skill.quality)
                } else if (item.equipment) {
                    let equipmentLvl: number = equipmentHandle.getEquipmentLvl(maxTotal)
                    marketItem.item = equipmentHandle.create(player,null, rand, { lucky: player.market.lvl * 20, equipmentLvl, minQuality: 1 })
                    diamond = utils.getIncValues(item.diamond, marketItem.item.equipment.quality)
                } else if (item.pet) {
                    let petConfig = gameConfigHandle.gameConfig.pet
                    let petID: number = utils.getOneFromArray(petConfig.callPetList, rand)
                    let floor = 5 + player.market.lvl * 2
                    if (floor > 50) {
                        floor = 50
                    }
                    marketItem.item = petHandle.create(petID, floor, rand, { lvl: 20, quality: 1 })
                    let { totalStar1, totalStar2 } = petHandle.getTotalStar(marketItem.item)
                    diamond = utils.getIncValues(item.diamond, totalStar1)
                }
                // if (item.gold && rand.intn(100) < 30) {
                //     marketItem.needGold = utils.getIncValues(item.gold, amount)
                // } else {
                let discount = 100
                let monthDiscount = 100 //月卡折扣
                if (rand.intn(100) < 45) {
                    discount = utils.getRandomNumber(65, 90, rand)
                    // if (rand.intn(100) < 35) {
                    //     monthDiscount = discount - 15
                    // }
                }
                marketItem.discount = discount
                marketItem.monthDiscount = monthDiscount
                marketItem.needDiamond = diamond
                // }
                if (item.item) {
                    marketItem.itemId = item.item
                } else if (item.items) {
                    marketItem.itemId = utils.getOneFromArray(item.items, rand)
                }
                marketItem.amount = amount
                data.push(marketItem)
            }
        })
        player.market.items = data
    }


    addExp(player: PlayerModel, exp: number) {
        player.market.exp += exp
        let marketConfig = gameConfigHandle.gameConfig.market
        let needExps = marketConfig.lvlupNeedExp
        let needExp = utils.getIncValues(needExps, player.market.lvl)
        if (player.market.exp >= needExp) {
            player.market.lvl += 1
            player.market.exp -= needExp
        }
        return
    }

}

let marketHandle = new MarketHandle()
export default marketHandle

